const obj = {
    name: '李四'
}
function test() {
    console.log(this, 'this')  //obj
}
test.call(obj)

/**
 * cxt:{
 *    [Symbol('key)]:()=>{}
 * }
*/

Function.prototype.myCall = function (obj, ...args) {
    if (typeof this !== 'function') return new Error('调用对象必须是函数')
    const cxt = obj !== null && obj !== undefined ? obj : globalThis
    const key = Symbol('key')
    cxt[key] = this
    const result = cxt[key](...args)
    delete cxt[key]
    return result
}

function test1(a = 1, b = 2) {
    console.log(a)
    console.log(this.a, this.b)
}
const obj1 = {
    a: 1,
    b: 2,
    // [Symbol('key')]: function test1(a = 1, b = 2) {
    //     console.log(a)
    //     console.log(this.a, this.b)
    // }
}
test1.myCall(obj1, 'abc')

// const p = test.call(obj,params1,params2,params3,...)
const pCall = test.myCall1(obj,params1,params2,params3)
Function.prototype.myCall1 = function(obj,...argus){
    if(typeof this !== 'function') return 
    const cxt = obj ?? globalThis
    const key = Symbol('key')
    cxt[key] = this
    const result = cxt[key](...argus)
    delete cxt[key]
    return result
}


// const pApply = test.apply(obj,[params1,params2,params3])
const pApply = test.myApply(obj,[params1,params2,params3])
Function.prototype.myApply = function(obj,params){
    if(typeof this !== 'function') return 
    const cxt = obj ?? globalThis
    const key = Symbol('key')
    cxt[key] = this
    const result = cxt[key](...params)
    delete cxt[key]
    return result
}

